//
//  NFSEasyChatHelper.m
//  NetEaseFinancial
//
//  Created by 吴玉峰 on 8/30/15.
//  Copyright (c) 2015 吴玉峰. All rights reserved.
//

#import "WYFEasyChatHelper.h"

#import "WYFShareHolder.h"
#import "YXApi.h"

static const CGFloat SHARE_IMG_COMPRESSION_QUALITY = 0.5;

@implementation WYFEasyChatHelper

- (void)sendToEasyChatFriend:(WYFShareHolder *)holder {
    [self shareOnEasyChatBase:kYXSceneSession holder:holder];
}

- (void)shareOnEasyChatMoment:(WYFShareHolder *)holder {
    [self shareOnEasyChatBase:kYXSceneTimeline holder:holder];
}

- (void)shareOnEasyChatBase:(enum YXScene)scene holder:(WYFShareHolder *)holder {
    __weak __typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *imgUrl = [NSURL URLWithString:holder.imgUrl];
        __block UIImage *thumbImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
        dispatch_sync(dispatch_get_main_queue(), ^{
            __strong __typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf p_shareWithThumbImage:holder thumbImage:thumbImage scene:scene];
        });
    });
}

- (void)p_shareWithThumbImage:(WYFShareHolder *)holder thumbImage:(UIImage *)thumbImage scene:(enum YXScene)scene {
    YXMediaMessage *message = [YXMediaMessage message];
    message.title = holder.title;
    message.description = holder.desc;
    message.thumbData = UIImageJPEGRepresentation(thumbImage, SHARE_IMG_COMPRESSION_QUALITY);

    YXWebpageObject *ext = [YXWebpageObject object];
    ext.webpageUrl = holder.webUrl;

    message.mediaObject = ext;
    //    message.mediaTagName = @"EasyChat_TAG_JUMP_SHOWRANK";

    SendMessageToYXReq *req = [[SendMessageToYXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    [YXApi sendReq:req];
}

+ (instancetype)sharedInstance {
    static WYFEasyChatHelper *helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[WYFEasyChatHelper alloc] init];

    });
    return helper;
}

- (instancetype)init {

    static id obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        obj = [super init];
        if (obj) {
        }

    });
    self = obj;
    return self;
}

@end
